package cn.cxion.chat.app.view.popupview;

import android.content.Context;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.lqr.optionitemview.OptionItemView;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.util.XPopupUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.cxion.chat.R;
import cn.cxion.chat.app.bean.AppointmentTimes;
import cn.cxion.chat.app.util.LogUtil;
import cn.cxion.chat.app.util.UiUtils;

import static com.lxj.xpopup.enums.PopupAnimation.TranslateFromBottom;

public class AppointmentTimePopupView extends BottomPopupView {

    @Bind(R.id.cancle)
    TextView cancle;
    @Bind(R.id.ok)
    TextView ok;
    @Bind(R.id.op_suretime)
    OptionItemView opSuretime;
    @Bind(R.id.switch_suretime)
    Switch switchSuretime;
    @Bind(R.id.ll_suretime)
    LinearLayout llSuretime;
    @Bind(R.id.selecttime)
    OptionItemView selecttime;
    AppointmentTimes appointmentTimes;
    onDateSelectListener listener;


    public AppointmentTimePopupView(@NonNull Context context) {
        super(context);
    }

    public AppointmentTimePopupView(@NonNull Context context, AppointmentTimes appointmentTimes) {
        super(context);
        if(appointmentTimes!=null)
        this.appointmentTimes = appointmentTimes.clone();
    }

    //设置监听
    public void setListener(onDateSelectListener listener) {
        this.listener = listener;
    }


    @Override
    protected int getImplLayoutId() {
        return R.layout.popup_appointmentcustomer;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        initView();
    }

    //初始化界面
    private void initView() {
        ButterKnife.bind(this);
        if (appointmentTimes == null) {
            appointmentTimes = new AppointmentTimes();
        } else {
            if (appointmentTimes.isSureTime()) {
                switchSuretime.setChecked(true);
            } else {
                switchSuretime.setChecked(false);
            }
            UiUtils.setRightText(appointmentTimes.getTimeText(),selecttime);
        }
    }

    @Override
    protected void onShow() {
        super.onShow();

        switchSuretime.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                appointmentTimes.setSureTime(isChecked);
            }
        });
    }

    @Override
    protected void onDismiss() {
        super.onDismiss();
    }

    @Override
    protected int getMaxHeight() {
        return (int) (XPopupUtils.getWindowHeight(getContext()) * 0.75);
    }

    @OnClick({R.id.cancle, R.id.ok, R.id.op_suretime, R.id.ll_suretime, R.id.selecttime})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cancle:
                dismiss();
                break;
            case R.id.ok:
                listener.selected(appointmentTimes);
                dismiss();
                break;
            case R.id.op_suretime:
                changeSwith();
                break;
            case R.id.ll_suretime:
                changeSwith();
                break;
            case R.id.selecttime:
                showDate();
                break;
        }
    }

    private void changeSwith() {
        if (switchSuretime.isChecked()) {
            appointmentTimes.setSureTime(false);
            switchSuretime.setChecked(false);
        } else {
            appointmentTimes.setSureTime(true);
            switchSuretime.setChecked(true);
        }
    }

    private void showDate() {
        DatePickPopupView  datePickPopupView = new DatePickPopupView(getContext(),appointmentTimes);
        datePickPopupView.setOnDateSelectListener(new DatePickPopupView.DateSelectListener() {
            @Override
            public void selectDate(AppointmentTimes appointmentTimes) {
                selecttime.setRightText(appointmentTimes.getTimeText());
            }
        });
        new XPopup.Builder(getContext())
                .popupAnimation(TranslateFromBottom)
                .asCustom(datePickPopupView/*.enableDrag(false)*/)
                .show();
    }

    public interface onDateSelectListener {
        void selected(AppointmentTimes appointmentTimes);
    }
}
